package cn.lyd.pet.controller;

import cn.lyd.base.enm.SearchMasterMsgStateEnum;
import cn.lyd.base.jwt.UserInfo;
import cn.lyd.base.until.AjaxResult;
import cn.lyd.base.until.BussinessException;
import cn.lyd.base.until.PageList;
import cn.lyd.base.until.ThreadLocalUtil;
import cn.lyd.org.domain.Shop;
import cn.lyd.pet.dto.AcceptDto;
import cn.lyd.pet.dto.SearchMasterMsgDto;
import cn.lyd.pet.service.ISearchMasterMsgService;
import cn.lyd.pet.domain.SearchMasterMsg;
import cn.lyd.pet.query.SearchMasterMsgQuery;
import cn.lyd.user.domain.Logininfo;
import cn.lyd.user.domain.User;
import cn.lyd.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    public IUserService userService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping()
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     * 发布寻主
     * @param dto
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsgDto dto) {
        return searchMasterMsgService.publish(dto);
    }


    /**
     * 前台分页查询数据
     * 这个接口是用户在个人中心查看自己发布的所有消息
     * @param query
     *  @return PageList 分页对象
     */
    @PostMapping("/front/list")
    public PageList<SearchMasterMsg> frontList(@RequestBody SearchMasterMsgQuery query) {
//        UserInfo userInfo = LoginContext.getUserInfo();
        UserInfo userInfo = ThreadLocalUtil.getUserInfo();
        Long loginInfoId = userInfo.getLogininfo().getId();
        //通过登录信息中的logininfoId找到的当前登录人
        User user = userService.findByLoginInfoId(loginInfoId);
        //将当前登录人的user_id设置到sql
        query.setUser_id(user.getId());
        return searchMasterMsgService.queryByPage(query);
    }


    /**
     * 店铺查询 待处理数据 寻主消息 自动匹配到商家  分配给自己店铺的消息   ==已审核==待处理
     * @param query
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query) {
        // 获取当前店铺id 审核成功 店铺id=shopId
//        UserInfo userInfo = LoginContext.getUserInfo();
        UserInfo userInfo = ThreadLocalUtil.getUserInfo();
        Logininfo logininfo = userInfo.getLogininfo();
        //把登陆人信息的shopid传到sql里   只要该店铺人员才有权限看到
        if (logininfo.getShop()!=null){
            Shop shop = logininfo.getShop();
            query.setShop_id(shop.getId());
        } else {
            query.setShop_id(null);
        }
        //1是待审核，把寻主消息为1的状态传到sql里
        query.setState(SearchMasterMsgStateEnum.AUDIT_SUCCESS.getCode());
        return searchMasterMsgService.queryByPage(query);
    }


    /**
     * 寻主池 寻主消息未匹配到商家
     * @param query
     * @return
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query) {
        // 审核成功 店铺id = null
        query.setState(1);
        return searchMasterMsgService.queryByPage(query);
    }



    /**
     * 已完成 商家对寻主 已经进行收购
     * @param query
     * @return
     */
    @PostMapping("/finish")
    public AjaxResult  finish(@RequestBody SearchMasterMsgQuery query) {
        // 状态为已完成 state = 1 & 店铺id = shopId
        // UserInfo userInfo = LoginContext.getUserInfo();
        UserInfo userInfo = ThreadLocalUtil.getUserInfo();
        Logininfo logininfo = userInfo.getLogininfo();
        Shop shop = logininfo.getShop();
        if(shop!=null){
            query.setShop_id(shop.getId());
            //4已完成
            query.setState(4);
            AjaxResult success = AjaxResult.me().success(searchMasterMsgService.queryByPage(query));
            return success;
        }else {
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMsg("您不是店铺人员！")
                    .setResultObj(new PageList<SearchMasterMsg>());
        }

    }


    /**
     * 接单
     * @param dto
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody  AcceptDto dto) {
        try {
            searchMasterMsgService.accept(dto);
            return new AjaxResult();
        }catch (BussinessException e){
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！");
        }



    }

    /**
     * 拒单
     * @param id
     * @return
     */
    @GetMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id)
    {
        searchMasterMsgService.reject(id);
        return AjaxResult.me().success();
    }

    /**
     * 发布服务
     * @param product_id     服务id
     * @return
     */
    @GetMapping("/sendServer/{product_id}")
    public AjaxResult sendServer(@PathVariable("product_id")Long product_id) {
         searchMasterMsgService.sendServer(product_id);
        return AjaxResult.me().success();
    }


}
